//
//  ImageTableViewCell.m
//  GIFShow
//
//  Created by lanou11air029 on 15/3/30.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "ImageTableViewCell.h"
#import "MYImageModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "MySettingOptions.h"
#import "rightTagLabel.h"
#import "NSString+stampToStr.h"
@interface ImageTableViewCell ()
@property (nonatomic, strong) UIImageView * userHeadIcon;
@property (nonatomic, strong) UILabel * userNameLabel;
@property (nonatomic, strong) UILabel * mainLabel;
@property (nonatomic, strong) UIView * mainBackgroundView;

//@property (nonatomic, strong) CustomButton * dingButton;
//@property (nonatomic, strong) CustomButton * moreButton;
//@property (nonatomic, strong) UIButton * shareButton;
//@property (nonatomic, strong) CustomButton * favourButton;


@end

@implementation ImageTableViewCell



- (void)awakeFromNib {
    // Initialization code
}
-(void)prepareForReuse{
    [self.dingButton setSelected:NO];
    [self.favourButton setSelected:NO];
    [super prepareForReuse];
    
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self setupSubviews];
    }
    return self;
}

-(void)setGifModel:(GifModel *)gifModel{

    if (_gifModel != gifModel) {
        _gifModel = gifModel;
        self.backgroundColor = [UIColor clearColor];
        NSDictionary * userInfo = _gifModel.userinfo;
        NSArray * imageExtra = _gifModel.imagelist;
        NSString * width = [imageExtra.lastObject valueForKey:@"thumb_w"];
        NSString * height = [imageExtra.lastObject valueForKey:@"thumb_h"];
        NSString * userUrlStr = [userInfo objectForKey:@"avatar"];
        [UIView animateWithDuration:0.1 animations:^{
            NSURL * userHeadUrl = [NSURL URLWithString:userUrlStr];
            [self.userHeadIcon sd_setImageWithURL:userHeadUrl placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
            self.userNameLabel.text = [userInfo objectForKey:@"nickname"];
            NSUInteger style = _gifModel.art_id.integerValue % 3;
            rightTagLabel * rightLabel = [[rightTagLabel alloc]initWithStyle:style];
            [self.mainBackgroundView addSubview:rightLabel];
    
         //日期

            self.createDateLabel.text = [NSString translateTimestamp:self.gifModel.pub_time.floatValue];
            
            self.mainImageView.frame = CGRectMake(self.bounds.size.width*0.05, self.bounds.size.width*0.2+2, self.bounds.size.width*0.9, self.bounds.size.width*0.9/width.floatValue * height.floatValue);
            self.mainLabel.frame = CGRectMake(self.bounds.size.width*0.05, self.mainImageView.frame.size.height+self.mainImageView.frame.origin.y + 5, self.bounds.size.width*0.9,30);
            //图片地址
            NSString * thumbStr = [imageExtra.lastObject valueForKey:@"thumb"];
            [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:thumbStr] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
            self.mainLabel.text = _gifModel.content;
            MySettingOptions * option = [MySettingOptions defaultOptions];
            float fontSize = option.contentFontValue;
            NSDictionary * textOption = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
            CGRect textFrame = [_gifModel.content boundingRectWithSize:CGSizeMake(self.contentView.bounds.size.width*0.9, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:textOption context:nil];
            self.mainLabel.frame = CGRectMake(self.mainLabel.frame.origin.x, self.mainLabel.frame.origin.y, textFrame.size.width, textFrame.size.height);
            
            // 顶
            CGRect buttonFrame = self.mainLabel.frame;
            self.dingButton.frame = CGRectMake(self.mainImageView.frame.origin.x, buttonFrame.origin.y+buttonFrame.size.height, self.mainBackgroundView.frame.size.width*0.28, self.mainBackgroundView.frame.size.width*0.12);
            NSInteger dingValue = gifModel.ding.intValue;
            [self.dingButton setTitle:[NSString stringWithFormat:@"%d",(int)dingValue] forState:UIControlStateNormal];
            [self.dingButton setTitle:[NSString stringWithFormat:@"%ld",(long)dingValue+1] forState:UIControlStateSelected];
            
            
            
            self.favourButton.frame = CGRectMake(self.dingButton.frame.origin.x + self.dingButton.frame.size.width, buttonFrame.origin.y+buttonFrame.size.height, self.mainBackgroundView.frame.size.width*0.28, self.mainBackgroundView.frame.size.width*0.12);
            NSInteger favourValue = gifModel.comment_sum.intValue;
            [self.favourButton setTitle:[NSString stringWithFormat:@"%d",(int)favourValue] forState:UIControlStateNormal];
            [self.favourButton setTitle: [NSString stringWithFormat:@"%ld",(long)favourValue+1] forState:UIControlStateSelected];
            
            self.shareButton.frame = CGRectMake(self.favourButton.frame.origin.x + self.favourButton.frame.size.width, buttonFrame.origin.y+buttonFrame.size.height, self.mainBackgroundView.frame.size.width*0.12, self.mainBackgroundView.frame.size.width*0.12);
            self.moreButton.frame = CGRectMake(self.mainImageView.frame.origin.x + self.mainImageView.frame.size.width - self.mainBackgroundView.frame.size.width*0.13, buttonFrame.origin.y+buttonFrame.size.height, self.mainBackgroundView.frame.size.width*0.12, self.mainBackgroundView.frame.size.width*0.12);
            self.dingButton.tag = self.indexPath.row + 100;
            self.favourButton.tag = self.indexPath.row + 101;
            self.moreButton.tag = self.indexPath.row + 102;
            self.shareButton.tag = self.indexPath.row + 103;
            
            CGRect mainBackFrame = self.mainBackgroundView.frame;
            mainBackFrame.size.height = self.mainLabel.frame.origin.y+self.mainLabel.frame.size.height+self.dingButton.frame.size.height + 10;
            mainBackFrame.size.width = self.bounds.size.width - 4;
            self.mainBackgroundView.frame = mainBackFrame;
            
         }];
    }
}



-(void)setupSubviews{
    MySettingOptions * option = [MySettingOptions defaultOptions];
    CGRect contentViewBounds = self.contentView.bounds;
    contentViewBounds.origin = CGPointMake(2, 2);
    contentViewBounds.size = CGSizeMake(self.contentView.bounds.size.width - 4, self.contentView.bounds.size.height - 4);
    self.mainBackgroundView = [[UIView alloc]initWithFrame:contentViewBounds];
    self.mainBackgroundView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.mainBackgroundView.layer.borderWidth = 1;
    self.mainBackgroundView.layer.cornerRadius = 5;
    [self.contentView addSubview:self.mainBackgroundView];
    self.mainBackgroundView.layer.masksToBounds = YES;
    self.mainBackgroundView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
    //用户头像
    self.userHeadIcon = [[UIImageView alloc]initWithFrame:CGRectMake(contentViewBounds.size.width*0.05, contentViewBounds.origin.y+self.bounds.size.width*0.03, contentViewBounds.size.width*0.13, contentViewBounds.size.width*0.13)];
    self.userHeadIcon.layer.cornerRadius = contentViewBounds.size.width*0.065;
    self.userHeadIcon.layer.masksToBounds = YES;
    self.userHeadIcon.layer.borderColor = [UIColor colorWithRed:0.7 green:0.8 blue:1 alpha:1].CGColor;
    self.userHeadIcon.layer.borderWidth = 1;
    
    [self.mainBackgroundView addSubview:self.userHeadIcon];
    //用户名
    
    self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.userHeadIcon.frame.origin.x+self.userHeadIcon.frame.size.width+self.contentView.frame.size.width*0.05, self.userHeadIcon.frame.origin.y, contentViewBounds.size.width * 0.7, 24)];
    self.userNameLabel.text = @"某人";
    self.userNameLabel.font = [UIFont systemFontOfSize:option.userFontValue];
    [self.mainBackgroundView addSubview:self.userNameLabel];
    // 视频创建日期
    self.createDateLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y+self.userNameLabel.frame.size.height, self.userNameLabel.frame.size.width, self.userNameLabel.frame.size.height)];
    self.createDateLabel.font = [UIFont systemFontOfSize:option.otherFontValue];
    self.createDateLabel.textColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.25 alpha:0.9];
    
    [self.mainBackgroundView addSubview:self.createDateLabel];
    
    self.mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2, 0, self.bounds.size.width*0.9, self.bounds.size.height - 4)];
    self.mainImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.mainImageView.clipsToBounds = YES;
    [self.contentView addSubview:self.mainImageView];
    

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //内容
    self.mainLabel = [[UILabel alloc]initWithFrame:CGRectMake(2, 2, self.bounds.size.width*0.9, 40)];
    self.mainLabel.font = [UIFont systemFontOfSize:option.contentFontValue];
    [self.contentView addSubview:self.mainLabel];
    self.mainLabel.numberOfLines = 0;
    [self.mainLabel sizeToFit];
    // 顶

    self.dingButton = [CustomButton buttonWithType:UIButtonTypeCustom];
    [self.dingButton setTitle:@"like" forState:UIControlStateNormal];
    [self.dingButton setImage:[UIImage imageNamed:@"xihuanmiaobian"] forState:UIControlStateNormal];
    [self.dingButton setImage:[UIImage imageNamed:@"xihuantianse"] forState:UIControlStateSelected];
    [self.dingButton setTintColor:[UIColor redColor]];
    
    self.dingButton.frame = CGRectMake(0, 0, 90, 40);
    [self.dingButton setTitleColor:[UIColor colorWithRed:250/255.0 green:64/255.0 blue:64/255.0 alpha:0.95] forState:UIControlStateNormal];
    [self.dingButton setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.mainBackgroundView addSubview:self.dingButton];
    // 分享
    self.favourButton = [CustomButton buttonWithType:UIButtonTypeCustom];
    [self.favourButton setTitle:@"favor" forState:UIControlStateNormal];
    [self.favourButton setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
    [self.favourButton setImage:[UIImage imageNamed:@"shoucang1"] forState:UIControlStateSelected];

    self.favourButton.frame = CGRectMake(70, 0, 90, 40);
    [self.favourButton setTitleColor:[UIColor colorWithRed:240/255.0 green:160/255.0 blue:73/255.0 alpha:0.95] forState:UIControlStateNormal];
    [self.favourButton setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.mainBackgroundView addSubview:self.favourButton];


    // 其他
    self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.moreButton setTitle:@"" forState:UIControlStateNormal];
    [self.moreButton setImage:[UIImage imageNamed:@"gengduo"] forState:UIControlStateNormal];
    self.moreButton.frame = CGRectMake(210, 0, 40, 40);
    [self.moreButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.moreButton setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.mainBackgroundView addSubview:self.moreButton];
    
  

    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.shareButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    self.shareButton.imageView.contentMode = UIViewContentModeScaleAspectFit ;
    self.shareButton.frame = CGRectMake(140, 0, 40, 40);
    
//    [self.shareButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.shareButton setTintColor:[UIColor colorWithRed:0.3 green:0.5 blue:0.1 alpha:1]];
    [self.mainBackgroundView addSubview:self.shareButton];

    
}


-(UIImage *)scaleImage:(UIImage *)image WithSize:(CGSize )size{
    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [image drawInRect:rect];
    UIImage * returnImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return returnImg;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
